package chapter5.event2;

import java.util.Dictionary;
import java.util.Hashtable;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;

public class Activator implements BundleActivator, EventHandler {
	
	//final static String[] topics = new String[]{"*"};
	final static String[] topics = new String[]{"org/osgi/framework/ServiceEvent/REGISTERED", "com/foo/bar"};
	
	
	
	
	@Override
	public void start(BundleContext context) throws Exception {
		Dictionary<String, Object> prop = new Hashtable<String, Object>();
		prop.put(EventConstants.EVENT_TOPIC, topics);
		
		context.registerService(EventHandler.class.getName(), this, prop);
	}
	
	@Override
	public void stop(BundleContext context) throws Exception {
		
	}

	@Override
	public void handleEvent(Event event) {
		System.out.println("\r\n[Event Received]Topic : " + event.getTopic());
		for(String key : event.getPropertyNames()){
			System.out.println("\t[Property] " + key + " : " + event.getProperty(key));
		}
	}
	
	
	

}
